//
//  HomeViewController.m
//  CarCare
//
//  Created by ileo on 14/12/24.
//  Copyright (c) 2014年 baozun. All rights reserved.
//

#import "MessageViewController.h"
#import "MessageTableViewCell.h"
#import "MessageMgr.h"
#import "CalendarMgr.h"
#import <CoreLocation/CoreLocation.h>
#import "BaseWebViewController.h"

#import "RadarPictureViewController.h"

#import "NetAdvertice.h"

#define TOP_SECTION 44

@interface MessageViewController()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, copy) NSArray *datas;

@property (nonatomic, strong) NSString *moneyStr;

@property (nonatomic, strong) UIImageView *carImageV;
@property (nonatomic, strong) UIImageView *carIconImageV;
@property (nonatomic, strong) UILabel *moneyL;
@property (nonatomic, strong) UILabel *locL;

@property (nonatomic, strong) NSString *locStr;

@property (nonatomic, strong) UIView *newsTipV;

@end

@implementation MessageViewController

-(void)viewDidLoad{
    
    [super viewDidLoad];
    
    self.title = @"消息";
    
    __weak __typeof(self) wself = self;
    
    self.tableView.frame = CGRectMake(0, 0, __MAIN_WIDTH, __MAIN_HEIGHT_64);
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    [MessageMgr loadMessageSuccess:^(NSArray *datas) {
        wself.datas = datas;
        [wself.tableView reloadData];
    } fail:^{
        
    }];
    

}

-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    [[MessageMgr sharedInstance] readAll];
}

-(void)didReceiveMemoryWarning{
    [super didReceiveMemoryWarning];
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
}

-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
}

#pragma mark - load

-(void)loadMoreTopFinish:(void (^)(CGFloat))finish withScrollView:(UIScrollView *)scrollView{
    __weak __typeof(self) wself = self;
    [MessageMgr loadMessageSuccess:^(NSArray *datas) {
        
        wself.datas = datas;
        [wself.tableView reloadData];
        if(finish)finish(0);
    } fail:^{
        if(finish)finish(0);
    }];
}

#pragma mark - tableView

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.datas.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return [MessageTableViewCell getCellHeight];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *cellID1 = @"cell1";
    
    UITableViewCell *cell;

    cell = [tableView dequeueReusableCellWithIdentifier:cellID1];
    if (!cell) {
        cell = [[MessageTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID1];
    }
    AdverticePageModel *model = self.datas[indexPath.row];
    ((MessageTableViewCell *)cell).model = model;
    
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    AdverticePageModel *model = self.datas[indexPath.row];
    [[StatisticsMgr sharedInstance] addClickID:[NSString stringWithFormat:@"%@-%@",APP_MESSAGE,model.title] description:[NSString stringWithFormat:@"首页通知广告位跳转的消息页面:%@",[tableView cellForRowAtIndexPath:indexPath].description]];
    [SCREEN viewController:self pushWebViewControllerWithModel:model];
}

@end